package com.echat.elocation.protocol;

import android.annotation.SuppressLint;
import android.location.Location;

import com.echat.elocation.connect.NettyClient;
import com.echat.elocation.constant.ProtocolConstant;
import com.echat.elocation.event.UserInfo;
import com.echat.elocation.protocol.ITerminalRequest;
import com.echat.elocation.protocol.code.MsgEncoder;
import com.echat.elocation.protocol.entity.PackageMsg;
import com.echat.elocation.protocol.entity.send.HeartbeatMsg;
import com.echat.elocation.protocol.entity.send.LocationUpLoadMsg;
import com.echat.elocation.protocol.entity.send.TerminalAuthenticationMsg;
import com.echat.elocation.protocol.entity.send.TerminalRegisterMsg;

import java.text.SimpleDateFormat;

/**
 *
 * @author lihao
 *
 * @date 2018/7/11 9:25
 */
public class TerminalRequest implements ITerminalRequest {

    private String mPhoneNumber;
    private String mTerminalId;
    private String mCarNumber;
    private int flowId;

    private MsgEncoder mEncoder;

    public TerminalRequest(UserInfo info) {
        initInfo(info);
        mEncoder = new MsgEncoder();
    }

    private void initInfo(UserInfo info) {
        mPhoneNumber = info.getPhoneNumber();
        mTerminalId = info.getTerminalId();
        mCarNumber = info.getCarNumber();
        flowId = 0;
    }

    @Override
    public void onTerminalRegister() {
        try {
            // 设置消息头数据
            TerminalRegisterMsg msg = new TerminalRegisterMsg();
            PackageMsg.MsgHeader header = new PackageMsg.MsgHeader();
            header.setMsgId(ProtocolConstant.msg_id_terminal_register);
            header.setPhoneNumber(mPhoneNumber);
            header.setFlowId(flowId());
            msg.setMsgHeader(header);

            // 设置消息体数据
            msg.setProvinceId(0);
            msg.setCityId(0);
            msg.setManufacturerId("0");
            msg.setTerminalType("0");
            msg.setTerminalId(mTerminalId);
            msg.setLicensePlateColor(1);
            msg.setLicensePlate(mCarNumber);

            byte[] data = mEncoder.encodeTerminalRegisterMsg(msg);
            NettyClient.getInstance().sendMessage(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onTerminalAuth(String authCode) {
        try {
            // 设置消息头数据
            TerminalAuthenticationMsg msg = new TerminalAuthenticationMsg();
            PackageMsg.MsgHeader header = new PackageMsg.MsgHeader();
            header.setMsgId(ProtocolConstant.msg_id_terminal_authentication);
            header.setPhoneNumber(mPhoneNumber);
            header.setFlowId(flowId());
            msg.setMsgHeader(header);

            // 设置消息体数据
            msg.setAuthCode(authCode);
            byte[] data = mEncoder.encodeTerminalAuthMsg(msg);
            NettyClient.getInstance().sendMessage(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onHeartbeat() {
        try {
            // 设置消息头数据
            HeartbeatMsg msg = new HeartbeatMsg();
            PackageMsg.MsgHeader header = new PackageMsg.MsgHeader();
            header.setMsgId(ProtocolConstant.msg_id_terminal_heart_beat);
            header.setPhoneNumber(mPhoneNumber);
            header.setFlowId(flowId());
            msg.setMsgHeader(header);

            byte[] data = mEncoder.encodeHeartbeatMsg(msg);
            NettyClient.getInstance().sendMessage(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("SimpleDateFormat")
    @Override
    public void onLocationUpload(Location location) {
        try {
            // 设置消息头数据
            LocationUpLoadMsg msg = new LocationUpLoadMsg();
            PackageMsg.MsgHeader header = new PackageMsg.MsgHeader();
            header.setMsgId(ProtocolConstant.msg_id_terminal_location_info_upload);
            header.setPhoneNumber(mPhoneNumber);
            header.setFlowId(flowId());

            // 设置消息体数据
            msg.setMsgHeader(header);
            msg.setWarningFlagField(0x00000000);
            msg.setStatusField(0x00000003);
            msg.setLatitude((float) (location.getLatitude() * 1E6));
            msg.setLongitude((float) (location.getLongitude() * 1E6));
            msg.setElevation((int) location.getAltitude());
            msg.setSpeed(location.getSpeed() * 36);
            msg.setDirection(location.getBearing());
            msg.setTime(new SimpleDateFormat("yyMMddHHmmss").format(location.getTime()));
            msg.setAppendMsgId(0x01);
            msg.setAppendMsgLength(4);
            msg.setAppendMsg(10);

            byte[] data = mEncoder.encodeLocationUploadMsg(msg);
            NettyClient.getInstance().sendMessage(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int flowId() {
        if (flowId == Integer.MAX_VALUE) {
            flowId = 0;
        }
        return flowId++;
    }
}
